package com.itheima.service;

import com.itheima.dao.StudentDao;
import com.itheima.domain.Student;

public class StudentService {
    static StudentDao head ;
    StudentDao studentDao = new StudentDao();

    {
        head= new StudentDao();
        Student s =new Student("001","尹周志","19","1999-12-1");
        this.addStudent(s);
        s =new Student("003","尹周志","19","1999-12-1");
        this.addStudent(s);
        s =new Student("002","尹周志","19","1999-12-1");
        this.addStudent(s);
        s =new Student("004","尹周志","19","1999-12-1");
        this.addStudent(s);
    }
    //添加数据并返回头指针
    public void addStudent(Student student){
        StudentService.head = studentDao.addStudent(student);
   //     System.out.println(head.getNextIndex().getStudent().getId());
    }


    //遍历
   public StudentDao findAllStudent(){
       StudentDao head = StudentService.head;

       if(verify(head)){
               return StudentService.head;
       }
       return null;
   }

   //遍历数组查找是否有相同的id
    public StudentDao isExists(String id) {
        StudentDao head = StudentService.head;
        //判断链表是否为空
        if(verify(head)) {//不为空执行循环
            while (head.getNextIndex() != null) {
                String nextId = head.getNextIndex().getStudent().getId();
                if(nextId.equals(id)){
                    System.out.println("找到了");
                    return head;
                }
                head = head.getNextIndex();
            }
        }
        return null;
    }

    //校验链表是否为空返回bool类型开关
    public boolean verify(StudentDao head){
        head = StudentService.head;//对接头指针
        //校验链表是否为空
        if(head.getNextIndex()==null){
            //    throw new NullPointerException("异常");
            System.out.println("没有数据");
            return false;//null链表返回的结果
        }

        return true;//不为空返回的结果
    }

    //删除节点
    public void deleteStudent(StudentDao deleteIndex) {
        studentDao.deleteStudent(deleteIndex);
    }

    //修改节点
    public void updateStudent(StudentDao index,Student student) {
        studentDao.updateStudent(index,student);
    }
}
